package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.TransferClassDao;
import com.example.domain.TransferClass;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.TransferClassService;
import com.example.tool.DateTool;

@Service
public class TransferClassServiceImpl implements TransferClassService {

    @Autowired
    TransferClassDao transferClassDao;

    /**
     * @describe 添加退班信息
     */
    @Override
    public Result addTransferClass(TransferClass transferClass) {
        if (transferClass != null) {
            // 获取系统当前时间作为记录创建时间
            transferClass.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        TransferClass transferClass1 = transferClassDao.save(transferClass);

        if (transferClass1 != null) {
            return ResultFactory.buildSuccessResult(transferClass1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新退班信息
     */
    @Override
    public Result updateTransferClass(TransferClass transferClass) {
        if (transferClass != null) {
            // 获取系统当前时间作为记录更新时间
            transferClass.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        TransferClass transferClass1 = transferClassDao.saveAndFlush(transferClass);

        if (transferClass1 != null) {
            return ResultFactory.buildSuccessResult(transferClass1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除退班信息
     */
    @Override
    public Result deleteTransferClassById(String transferClassId) {
        int row = transferClassDao.deleteByTransferClassId(Integer.parseInt(transferClassId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查詢所有退班信息
     */
    @Override
    public Result queryAllTransferClass() {
        List<TransferClass> list = transferClassDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询退班信息
     */
    @Override
    public Result queryTransferClassById(String transferClassId) {
        List<TransferClass> list = transferClassDao.findByTransferClassId(Integer.parseInt(transferClassId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加退班信息
     */
    @Override
    public Result addTransferClassByExcel(List<TransferClass> list) {
        List<TransferClass> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = transferClassDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据退班编号批量删除退班信息
     */
    @Override
    public Result deleteTransferClassByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = transferClassDao.deleteByTransferClassIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 退班信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
//		System.out.println(info[0]);
        if (info[0].equals("originalClass")) {

            list = transferClassDao.findByOriginalClassLike(info[1]);

        } else if (info[0].equals("transferStatus")) {

            list = transferClassDao.findByTransferStatusLike(info[1]);

        } else if (info[0].equals("transferReason")) {

            list = transferClassDao.findByTransferReasonLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = transferClassDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = transferClassDao.findByNickNameLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有用户转班信息
     */
    @Override
    public Result queryAllTransferClassAndUsersInfo() {
        // TODO 查询所有用户转班信息
        List<Map<String, Object>> list = transferClassDao.queryAllTransferClassAndUsersInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result selectTransferClassByUserId(Integer userId) {
        // TODO 根据学生ID查询学生转班信息
        List<Map<String, Object>> list = transferClassDao.selectTransferClassByUserId(userId);
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
